<?php
/**
 *
 * Base class for controller from which other controllers will extend.
 *
 */

use Doctrine\ORM;
class Bemigo_Controller_Action_Base extends Zend_Controller_Action{

	public $_facebook;

	protected $_fbcookie;

	public function init(){
		parent::init();
		$this->setCookie();
		$this->setFacebook();
	}

	/**
	 * @return \Doctrine\ORM\EntityManager $em;
	 */
	public function getEm(){
		return Zend_Registry::get("em");
	}

	protected function setFacebook(){
		$this->_facebook = new Bemigo_Service_Facebook(array(
					'appId'  => Zend_Registry::get("configuration")->facebook->app->id,
					'secret' => Zend_Registry::get("configuration")->facebook->app->secret,
				));
		if(!empty($this->_fbcookie['access_token'])){
			$this->_facebook->setAccessToken($this->_fbcookie['access_token']);
		}
	}

	protected function setCookie(){
		$this->_fbcookie = $this->_helper->FacebookCookie->getFacebookCookie();
	}

	public function checkFBAuth(){
		$user = $this->_facebook->getUser();
		if($user && !empty($this->_fbcookie)){
			return true;
		}
		$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'
	      ? 'https://'
	      : 'http://';
	    $currentUrl = $protocol . $_SERVER['HTTP_HOST'];
		$this->_redirect($currentUrl);
	}

	public function isLoggedIn(){
		$user = $this->_facebook->getUser();
		if($user && !empty($this->_fbcookie)){
			return true;
		}
		return false;
	}

}